version 8.2
capture clear
capture log close
set more off
set mem 1000m
set mat 800

/*
****prepare a dataset with the count of firms by d1a2:
use "Data/Enterprise surveys/Enterprise surveys_clean.dta"
gen one=1
egen count=sum(one), by(d1a2)
sort d1a2
tab d1a2
keep if d1a2!=d1a2[_n-1]
count
exit
browse d1a2 description count
clear

*I used that to create Correspondence Enterprise survey Nunn data.dta:

Note: for each d1a2, there may be several codes in the Nunn data 

*/

*Nunn data:
use "contract_intensity_IO_1997.dta"
sort industry_code
save "contract_intensity_IO_1997_clean.dta", replace

use "Correspondence Enterprise survey Nunn data.dta"
ren beacodenunn industry_code
sort industry_code
merge industry_code using "contract_intensity_IO_1997_clean.dta"
codebook _merge
keep d1a2 description count frac_lib_diff
ren frac_lib_diff frac_lib_diff1
sort d1a2
save "Contract intensity.dta", replace

*Note: I do the following because
*for each d1a2, there may be several codes in the Nunn data 

forvalues i=5(1) 19{
	clear
	use "Correspondence Enterprise survey Nunn data.dta"
	tostring var`i', replace
	ren var`i' industry_code
	sort industry_code
	merge industry_code using "contract_intensity_IO_1997_clean.dta"
	codebook _merge
	keep d1a2 description count frac_lib_diff
	ren frac_lib_diff frac_lib_diff`i'
	sort d1a2
	merge d1a2 using "Contract intensity.dta"
	drop _merge 
	sort d1a2 
	save "Contract intensity.dta", replace
	}
	

egen frac_lib_diff=rowmean(frac_lib_diff*)	
keep d1a2 description count frac_lib_diff

keep if d1a2!=.
save "Contract intensity.dta", replace



exit




